from math_verify import LatexExtractionConfig, parse, verify
from latex2sympy2_extended import NormalizationConfig


def accuracy_reward(completions,solution,**kwargs):
    contents = [completion[0]["content"] for completion in completions]
    rewards =[]
    for content,sol in zip(contents,solution):
        gold_parsed = parse(sol,extraction_mode="first_match",
                            extraction_config=[LatexExtractionConfig()])
        if gold_parsed:
            answer_parsed =parse(
                content,
                extraction_config=[
                    LatexExtractionConfig(
                        normalization_config=NormalizationConfig(
                            nits=False,
                            malformed_operators=False,
                            basic_latex=True,
                            equations=True,
                            boxed="all",
                            units=True,
                        ),
                        boxed_match_priority=0,
                        try_extract_without_anchor=False,
                    )
                ],
                extraction_mode="first_match"
            )
            reward = float(verify(anext,gold_parsed))
        else:
            reward = 0.5
            print("Warning: Failed to parse gold solution:",sol)
        rewards.append(reward)
    return rewards